import { Injectable } from "@nestjs/common";

@Injectable()
export class PlayerStatusService {
  async buildAct100(
    characterId: number,
    services: {
      characterLocationService?: any;
    }
  ) {
    let areaId = 2; // 新手村
    let areaName = "新手村";
    let playerCount = 0;
    let areaObj: any = null;

    try {
      if (services.characterLocationService) {
        const location =
          await services.characterLocationService.getPlayerLocation(
            characterId
          );
        if (location) {
          areaId = location.areaId;
          areaName = location.area?.name || "未知区域";
          areaObj = location.area;
          try {
            const onlinePlayers =
              await services.characterLocationService.getOnlinePlayersInArea(
                areaId
              );
            playerCount = Array.isArray(onlinePlayers)
              ? onlinePlayers.length
              : 0;
          } catch {}
        }
      }
    } catch {}

    return {
      act_id: 100,
      area_id: areaId,
      id: areaObj?.id,
      imgType: 16,
      name: areaName,
      num: playerCount,
      ss: 5023,
      type_id: 6,
    };
  }
  async buildAct300(
    characterId: number,
    services: {
      characterService: any;
      characterAttributeService: any;
      vipService?: any;
    }
  ) {
    const character = await services.characterService.getCharacterWithDetails(
      characterId
    );
    const basic =
      await services.characterAttributeService.getCharacterBasicAttributes(
        characterId
      );
    const resources = character?.resources || {
      gold: 0,
      silver: 0,
      bdSilver: 0,
      fc: 0,
      superHK: 0,
      tlz: 0,
      tlzs: 0,
      vipBag: 0,
    };

    let vipLvl = -1;
    try {
      const vipInfo = await services.vipService?.queryVipInfo(characterId, -2);
      vipLvl = vipInfo?.vipLvl ?? -1;
    } catch {}

    return {
      act_id: 300,
      bdSilver: resources.bdSilver,
      exp: basic?.exp || 0,
      fc: resources.fc,
      gold: resources.gold,
      hp: basic?.hp || 0,
      id: basic?.id,
      lvl: basic?.lvl || 0,
      maxHp: basic?.maxHp || 0,
      maxMp: basic?.maxMp || 0,
      mp: basic?.mp || 0,
      name: basic?.name || "",
      roleType: basic?.roleType || 0,
      sex: basic?.sex || 0,
      silver: resources.silver,
      superHK: resources.superHK,
      tlz: resources.tlz,
      tlzs: resources.tlzs,
      vip: vipLvl,
      vipBag: resources.vipBag,
    };
  }
}
